package com.neusoft.neu24.nep.filter.globalfilter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;


@Component
@Slf4j
public class CorsGlobalFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {


//        System.out.println("pre .......... ");
//        Mono<Void> filter = chain.filter(exchange);
////        在此启动了一个线程去执行远程调用
//        System.out.println("post ............... ");
//        return filter;

//        return chain.filter(exchange)
//                .then(Mono.fromRunnable(
////                        再次启动线程
//                        () -> log.info("Global Post Filter Execute...")
//                ));

       /*
        return chain.filter(exchange)
                .then(Mono.fromRunnable(
                        () -> log.info("Global Post Filter Execute...")
                ));
        */

        System.out.println(" 统一解决跨越请求问题..................   ");

        exchange.getResponse().setStatusCode(HttpStatus.OK);
        exchange.getResponse().getHeaders().add("Content-Type", "application/json;charset=UTF-8");
        exchange.getResponse().getHeaders().add("Access-Control-Allow-Origin", "*");
        exchange.getResponse().getHeaders().add("Access-Control-Allow-Headers", "*");
        exchange.getResponse().getHeaders().add("Access-Control-Allow-Methods", "*");
        exchange.getResponse().getHeaders().add("Access-Control-Allow-Credentials", "true");
        return  chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return   Ordered.HIGHEST_PRECEDENCE+2;
    }
}